﻿using Finance.Infrastructrue.Interface;
using Finance.API.Write.Application.Command.PayableManagementCommand;
using Finance.Domain;
using Finance.ErrorCode;
using MediatR;
using Finance.Infrastructrue.Implement;

namespace Finance.API.Write.Application.Hander.PayableManagementHander
{
    public class DelPayableManagementHander:IRequestHandler<DelPayableManagementCommand ,APIResult<int>>
    {
        DatabaseContext db;
        IBaseRepository<PayableManagement> payRep;
        ILogger<DelPayableManagementHander> logger;

        public DelPayableManagementHander(DatabaseContext db, IBaseRepository<PayableManagement> payRep, ILogger<DelPayableManagementHander> logger)
        {
            this.db = db;
            this.payRep = payRep;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(DelPayableManagementCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                var m = db.SqlSugar.Queryable<PayableManagement>().Where(a => a.PayableManagementId == request.PayableManagementId).First();
                m.IsDel = true;
                int mls=await db.SqlSugar.Updateable(m).ExecuteCommandAsync();

                result.Code = APIEnum.Ok;
                result.Message = "应付管理逻辑删除成功";
                result.Data = mls;
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                result.Code = APIEnum.Ok;
                result.Message = "应付管理逻辑删除失败" + ex.Message;
                return await Task.FromResult(result);
            }
        }
    }
}
